/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.*;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class ShoppingCartDAO {
private PreparedStatement pStmtSelectCount = null;
    private PreparedStatement pStmtSelectAll = null;
    private PreparedStatement pStmtSelect = null;
    private PreparedStatement pStmtInsert = null;
    private PreparedStatement pStmtDelete = null;
    private PreparedStatement pStmtDeleteAll = null;
    private PreparedStatement pStmtUpdate = null;
    private static ShoppingCartDAO shoppingCartDAO = null;

    public static ShoppingCartDAO singleton() {
        if (shoppingCartDAO == null) {
            shoppingCartDAO = new ShoppingCartDAO();
        }
        return shoppingCartDAO;
    }

    public ShoppingCartDAO() {
        try {
            pStmtSelectAll = DBConnection.getConnection().prepareStatement("SELECT * FROM ShoppingCarts WHERE UserId = ? LIMIT ?,?");
            pStmtSelect = DBConnection.getConnection().prepareStatement("SELECT * FROM ShoppingCarts WHERE ItemId = ? and UserId = ?");
            pStmtInsert = DBConnection.getConnection().prepareStatement("INSERT INTO ShoppingCarts(ItemId, UserId, ItemDesc, Price, Quantity) VALUES(?,?,?,?,?)");
            pStmtDelete = DBConnection.getConnection().prepareStatement("DELETE FROM ShoppingCarts WHERE UserId = ? AND ItemId = ?");
            pStmtDeleteAll = DBConnection.getConnection().prepareStatement("DELETE FROM ShoppingCarts WHERE UserId = ?");
            pStmtUpdate = DBConnection.getConnection().prepareStatement("UPDATE ShoppingCarts SET Quantity=? WHERE ItemId = ? AND userId = ?");
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int recordCount(){
        int totalRecord = 0;
        try {
            ResultSet rs = pStmtSelectCount.executeQuery();
            while (rs.next()) {
                totalRecord = rs.getInt(1);
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(CardTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return totalRecord;
    }

    public ArrayList<ShoppingCart> selectAll(int userId, int page) {
        ArrayList<ShoppingCart> lstShoppingCarts = new ArrayList<ShoppingCart>();
        int offset = (page-1) * WebConst.ROW_PER_PAGE;
        try {
            pStmtSelectAll.setInt(1, userId);
            pStmtSelectAll.setInt(2, offset);
            pStmtSelectAll.setInt(3, WebConst.ROW_PER_PAGE);
            ResultSet rs = pStmtSelectAll.executeQuery();
            while (rs.next()) {
                lstShoppingCarts.add(new ShoppingCart(rs.getInt(1), rs.getInt(2),
                        rs.getString(3), rs.getDouble(4), rs.getInt(5)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lstShoppingCarts;
    }

    public ShoppingCart select(int userId, int itemId) {
        ShoppingCart shoppingCart = null;
        try {
            pStmtSelect.setInt(1, itemId);
            pStmtSelect.setInt(2, userId);
            ResultSet rs = pStmtSelect.executeQuery();
            while (rs.next()) {
                shoppingCart = new ShoppingCart(rs.getInt(1), rs.getInt(2),
                        rs.getString(3), rs.getDouble(4), rs.getInt(5));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return shoppingCart;
    }

    public String insert(ShoppingCart shoppingCart) {
        String result = "";
        boolean bRet = false;
        try {
            pStmtInsert.setInt(1, shoppingCart.getItemId());
            pStmtInsert.setInt(2, shoppingCart.getUserId());
            pStmtInsert.setString(3, shoppingCart.getItemDesc());
            pStmtInsert.setDouble(4, shoppingCart.getPrice());
            pStmtInsert.setInt(5, shoppingCart.getQuantity());
            pStmtInsert.execute();
            bRet = true;
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = bRet ? "Successed" : "Failed";
        return result;
    }

    public String delete(int userId, int itemId) {
        String result = "";
        int numOfRows = 0;
        try {
            pStmtDelete.setInt(1, itemId);
            pStmtDelete.setInt(2, userId);
            numOfRows = pStmtDelete.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows > 0 ? "Successed" : "Failed";

        return result;
    }

    public String update(ShoppingCart shoppingCart) {
        String result = "";
        int numOfRows = 0;
        try {
            pStmtUpdate.setInt(1, shoppingCart.getQuantity());
            pStmtUpdate.setInt(2, shoppingCart.getItemId());
            pStmtUpdate.setInt(3, shoppingCart.getUserId());

            numOfRows = pStmtUpdate.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCartDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = numOfRows > 0 ? "Successed" : "Failed";

        return result;
    }
}
